
public class ThreadExample {
    public static void main(String[] args) {
        Thread thread = new Thread(new MyRunnable());
        thread.start(); // 启动子线程

        try {
            Thread.sleep(5000); // 主线程挂起5秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        thread.interrupt(); // 终止子线程
    }

    static class MyRunnable implements Runnable {
        public void run() {
            while(!Thread.currentThread().isInterrupted()) {
                System.out.println("thread is running!");

                try {
                    Thread.sleep(1000); // 子线程每秒输出一次
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt(); // 重新标记中断状态
                }
            }
        }
    }
}